﻿Imports DTO
Imports BUS
Imports System.IO
Imports System.Threading

Public Class frmThongTin

    Private thongTin As ThongTinDTO
    Private DsLinhVuc As List(Of LinhVucDTO)
    Private threadCapNhat As Thread
    Private Delegate Sub mHinhCho()

    Private Sub btnDongY_Click(sender As Object, e As EventArgs) Handles btnDongY.Click
        'showManHinhCho()
        'mhCho.ShowPopup()
        'ProgressPanel1.Show()
        SplashScreenManager1.ShowWaitForm()
        threadCapNhat = New Thread(AddressOf Me.luuTT)
        threadCapNhat.Start()

    End Sub

    Private Sub luuTT()
        'showManHinhCho()
        Dim tt As New ThongTinDTO
        tt.AnhDaiDien = chuyenAnhThanhByte(picImagThongTin.Image)
        tt.TenDonVi = txtDonVi.Text
        tt.DiaChi = txtDiaChi.Text
        tt.DienThoai = txtDienThoai.Text
        tt.Fax = txtFax.Text
        tt.TrangWEB = txtWeb.Text
        tt.Email = txtEmail.Text
        tt.MaSoThue = txtMaSoThue.Text
        tt.GPKD = txtGPKD.Text
        tt.MaLinhVuc = DsLinhVuc.Find(Function(lv As LinhVucDTO)
                                          Return lv.Ma = cbxLinhVuc.SelectedIndex + 1
                                      End Function).MaLinhVuc
        Dim kq As Integer = ThongTinBUS.capNhatTT(tt)
        If kq > 0 Then
            DevExpress.XtraEditors.XtraMessageBox.Show("Dữ liệu đã được ghi nhận", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
            SplashScreenManager1.CloseWaitForm()
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Dữ liệu không được ghi nhận", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Information)
            SplashScreenManager1.CloseWaitForm()
        End If
        threadCapNhat.Abort()

    End Sub


    Private Sub btnKetThuc_Click(sender As Object, e As EventArgs) Handles btnKetThuc.Click
        Me.Close()
    End Sub

    Private Sub frmThongTin_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        loadTT()
    End Sub

    Private Sub ganTTLenFrom()
        'cbxLinhVuc.Properties.DataSource = DsLinhVuc
        'cbxLinhVuc.Properties.DisplayMember = "TenLinhVuc"
        'cbxLinhVuc.Properties.ValueMember = "MaLinhVuc"
        For Each tt As LinhVucDTO In DsLinhVuc
            cbxLinhVuc.Properties.Items.Add(tt.TenLinhVuc.ToString())
        Next

        txtDonVi.Text = thongTin.TenDonVi
        txtDiaChi.Text = thongTin.DiaChi
        txtDienThoai.Text = thongTin.DienThoai
        txtFax.Text = thongTin.Fax
        txtWeb.Text = thongTin.TrangWEB
        txtEmail.Text = thongTin.Email
        cbxLinhVuc.SelectedIndex = DsLinhVuc.Find(Function(lv As LinhVucDTO)
                                                      Return lv.MaLinhVuc = thongTin.MaLinhVuc
                                                  End Function).Ma - 1

        txtMaSoThue.Text = thongTin.MaSoThue
        txtGPKD.Text = thongTin.GPKD
        picImagThongTin.Image = chuyenByteSangImage(thongTin.AnhDaiDien)
        SplashScreenManager2.CloseWaitForm()
    End Sub

    Private Function chuyenAnhThanhByte(image As Image) As Byte()
        Dim a As Byte()
        Try
            Dim ms As New MemoryStream()
            image.Save(ms, image.RawFormat)
            a = ms.GetBuffer()
            Return a
        Catch ex As Exception
        End Try
        Return thongTin.AnhDaiDien
    End Function

    Private Function chuyenByteSangImage(b As Byte()) As Image
        Dim img As Image
        Using ms As New MemoryStream(b, 0, b.Length)
            ms.Write(b, 0, b.Length)

            'Set image variable value using memory stream.
            img = Image.FromStream(ms, True)
        End Using
        Return img
    End Function

    Private Sub loadTT()
        SplashScreenManager2.ShowWaitForm()
        thongTin = ThongTinBUS.layThongTin()
        DsLinhVuc = LinhVucBUS.layDsLinhVuc()
        ganTTLenFrom()

    End Sub

End Class